﻿@model IEnumerable<Ivs.Core.Mapping.m_role>
@*@model IEnumerable<SketchTour.Models.MSRM00Models>*@
@{
    ViewBag.Title = "Index";
}
<script type="text/javascript">
    function setValueIdEdit(thisItem, hiddenID, intVal) {
        try {
            var checkAll = document.getElementById(thisItem);
            var listIdEdit = document.getElementById(hiddenID).value;

            if (checkAll.checked == false) {
                //alert("false");
                var strIdBefore = document.getElementById(hiddenID).value;
                var idRep = intVal + ";";
                //var strIdRep = '/' + idRep + '/i';
                var strReplace = strIdBefore.replace(idRep, "");
                $("#" + hiddenID).val(strReplace);
                var checkAll = document.getElementById('chkAllItem');
                if (checkAll.checked == true) {
                    checkAll.checked = false;
                }
            } else {
                // alert("True");
                if (listIdEdit.length > 0) {
                    var strIdBefore = document.getElementById(hiddenID).value;
                    var addData = strIdBefore + intVal + ";"
                    $("#" + hiddenID).val(addData);
                } else {
                    var addData = intVal + ";"
                    $("#" + hiddenID).val(addData);
                }
            }


        } catch (e)
            { alert(e); }
    }

    function submitAction(IdEdit, Action) {
        try {
            var listCheckboxValue = document.getElementById(IdEdit).value;
            var checked = $("#table_Role input[type=checkbox]:checked").length;
            if (Action == 'Edit') {
                if (checked == 0) {
                    alert("Vui long chon!");
                } else if (checked == 1) {
                    var Id = listCheckboxValue.split(";")[0]
                    redirectionToAction("MSRM00", Action, Id)
                }
                else if (checked > 1) {
                    alert("Chon chua dung!");
                }
            }
            // action Delete
            else if (Action == 'Delete') {
                //add list Id Delete
                if (checked == 0) {
                    alert("Vui long chon!");
                } else {

                    $.ajax({
                        url: 'MSRM00/Delete',
                        type: 'POST',
                        contentType: 'application/json;',
                        data: JSON.stringify({ id: listCheckboxValue }),
                        success: function (data) {

                            if (data.success) { 
                                window.location.href = data.redirectUrl;
                            }
                            else {
                                alert("False");
                            }
                        }
                    });

                }
            }
            // action Permission
            else if (Action == 'Permission') {
                if (checked == 0) {
                    alert("Vui long chon!");
                } else if (checked == 1) {
                    var Id = listCheckboxValue.split(";")[0]
                    redirectionToAction("MSRM00", Action, Id);
                }
                else if (checked > 1) {
                    alert("Chon chua dung!");
                }
            }


        } catch (e) {
            alert(e);
        }
    }

    function redirectionToAction(controller, action, id) {
        var url = "../../" + controller + "/" + action + "/"
        if (id != undefined)
            url += id;
        window.location = url;
    }

    function selectAll() {
        try {
            var checkAll = document.getElementById('chkAllItem');
            var table = document.getElementById('table_Role');
            var inputs = table.getElementsByTagName('input');
            var recordId = '';
            if (checkAll.checked == false) {

                for (var i = 0; i < inputs.length; i++) {
                    if (inputs[i].type == 'checkbox' && inputs[i].id != 'chkAllItem') {
                        inputs[i].checked = false;
                    }
                }
            }
            else {
                for (var i = 0; i < inputs.length; i++) {
                    if (inputs[i].type == 'checkbox') {
                        inputs[i].checked = true;
                    }
                    if (inputs[i].type == 'checkbox' && inputs[i].id != 'chkAllItem') {
                        inputs[i].checked = true;
                        recordId += inputs[i].id.replace('role_', '') + ";";
                    }
                }
            }
            $("#IdEdit").val(recordId);
        } catch (e) {
            alert(e);
        }
    }

    /*End Check All*/

</script>
<!-- main start-->
<div id="main">
    <!-- InstanceBeginEditable name="EditRegion1" -->
    <h2>
        Role Management</h2>
    <!-- InstanceEndEditable -->
    <div id="error" class="error">
    </div>
    <div id="Div1" class="success">
    </div>
    <!-- InstanceBeginEditable name="EditRegion2" -->
    <p>
    </p>
    <p>
    </p>
    @using (Html.BeginForm("Index", "MSRM00"))
    {
        
        <table class="inputForm">
            <tbody>
                <tr>
                    <th>
                        Role Name @*@Ivs.Core.Data.*@
                    </th>
                    <td>
                        @Html.TextBox("RoleName", string.Empty)
                    </td>
                </tr>
            </tbody>
        </table>
        <input name="btSearch" value="Search" class="btn60" type="submit">

    }
    <input name="btnAdd" type="button" value="Add" onclick="location.href='@Url.Action("Create", "MSRM00")'" />
    @*<input name="btnEdit" type="button" value="Edit" onclick="location.href='@Url.Action("Edit", "MSRM00", new { id=1})" />*@
    @Html.Hidden("hdfRecordId")
    @Html.Hidden("IdEdit")
    @*<input name="btnEdit" type="button" value="Edit" onclick="return getValueEdit('idF');" />*@
    @*  <input type="submit" id="Edit" name="btnSubmit" value="Edit" onclick="return getValueEdit('idF');" />
        <input name="btDelete" value="Delete" class="btn60" type="submit" />*@
    <input type="submit" id="Edit" name="btnSubmit" value="Edit" onclick="return submitAction('IdEdit','Edit');" />
    <input type="submit" id="Delete" name="btnSubmit" value="Delete" onclick="return submitAction('IdEdit','Delete');" />
    <input name="btPermission" value="Permission" class="btn80" type="submit" onclick="return submitAction('IdEdit','Permission');" />
    <div code="divList">
        <div code="divList">
            <div class="success">
                <p>
                </p>
            </div>
        </div>
    </div>
    <div style="overflow: auto; width: 100%">
        <table class="data-table" id="table_Role">
            <tbody>
                <tr bgcolor="D8E6FC">
                    <th style="text-align: center;" scope="col" class="th10">
                        @*<input type="checkbox" value="" class="classCheck" />*@
                        @Html.CheckBox("chkAllItem", false, new { onclick = "selectAll()" })
                    </th>
                    <th style="text-align: center;" scope="col" class="th100">
                        Role Name
                    </th>
                </tr>
                @foreach (var item in Model)
                {
                    <tr>
                        <td>
                            @*<input type="checkbox" value="@item.ID " class="classCheck" />*@
                            @Html.CheckBox("role_" + item.ID, false, new { onclick = "setValueIdEdit('role_" + item.ID + "'," + "'IdEdit'" + "," + item.ID + ")" })
                            @Html.Hidden("hidenRole_" + item.ID, item.ID)
                            @*
                            @Html.CheckBox("chkPODetail" + item.ID, false, new { onclick = "selectItem('chkPODetail" + item.ID + "')" })
                            @Html.Hidden("hdfPODetailId" + item.ID, item.ID)
                            *@
                        </td>
                        <td style="text-align: left;">
                            @Html.DisplayFor(modelItem => item.RoleName)
                        </td>
                    </tr>
                }
            </tbody>
        </table>
    </div>
    <div class="pager">
        <label class="disabled">
            &lt;&lt;</label><label class="disabled">&lt;</label><label class="current">1</label><label
                class="disabled">&gt;</label><label class="disabled">&gt;&gt;</label>
    </div>
    <!-- InstanceEndEditable -->
</div>
<!-- main end-->
